package com.hnkywl.sts.service.tjbb;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.djgl.GoodsesDao;
import com.hnkywl.sts.dao.djgl.JyddDao;
import com.hnkywl.sts.entity.djgl.Goodses;
import com.hnkywl.sts.entity.djgl.Jydd;
import com.hnkywl.sts.entity.zjgl.Bzjyj;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2015/12/18 0018.
 */
@Service
public class Ddcxservice {

    private JyddDao jyddDao;
    private GoodsesDao goodsesDao;

    public Ddcxservice() {
    }

    public String findAlljydd(String htbm, int pageNo, int pageSize) {
        Page page = this.jyddDao.findJyddjList(htbm, pageNo, pageSize);
        List<Jydd> list = page.getResult() == null ? new ArrayList() : page.getResult();
        String json = BaseUtils.fetchCollectionToJson((Collection) list, new String[]{"id", "order_sn", "seller_name", "buyer_name", "add_time", "pay_time", "goods_amount", "order_type"});
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":[" + json + "]}";
    }


    public String findGoodsesByJydd(Long jydd_id, int pageNo, int rows) {
        Page page = this.goodsesDao.findGoodsesList(jydd_id, pageNo, rows);
        String json = BaseUtils.fetchCollectionToJson((Collection) page.getResult(), new String[]{"purchase_contract_no", "production_contract_no", "goods_name", "real_price", "quantity", "measure", "brand", "spec", "metarial", "warehouse_name", "region_name", "resource_serial_number", "pc", "resource_serial_number_1"});
        List<Goodses> list = page.getResult();
        Double quantity = Double.valueOf(0.0D);
        Double measure = Double.valueOf(0.0D);
        if (list.size() != 0) {
            for (Goodses goodses : list) {
                quantity = Double.valueOf(goodses.getQuantity()) + quantity;
                measure = Double.valueOf(goodses.getMeasure()) + measure;
            }
            return "{\"total\":" + page.getTotalCount() + ",\"rows\":[" + json + "],\"footer\":[{\"purchase_contract_no\":\"合计:\",\"quantity\":" + quantity.toString() + ",\"measure\":" + measure.toString() + " }]}";
        } else {
            return "{\"total\":" + page.getTotalCount() + ",\"rows\":[" + json + "]}";
        }

    }

    public Jydd findJyddById(Long id) {
        return (Jydd) this.jyddDao.get(id);
    }

    @Autowired
    public void setJyddDao(JyddDao jyddDao) {
        this.jyddDao = jyddDao;
    }

    @Autowired
    public void setGoodsesDao(GoodsesDao goodsesDao) {
        this.goodsesDao = goodsesDao;
    }


}
